package com.nkym.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nkym.common.ErrorCode;
import com.nkym.constants.UserConstant;
import com.nkym.exception.BusinessException;
import com.nkym.mapper.InterfaceInfoMapper;
import com.nkym.mapper.UserInterfaceMapper;

import com.nkym.pojo.dto.interfaceInfo.InterfaceInfoQueryRequest;
import com.nkym.pojo.entity.InterfaceInfo;
import com.nkym.pojo.entity.User;
import com.nkym.pojo.entity.UserInterface;
import com.nkym.pojo.userInterface.UserInterfaceAssignRequest;
import com.nkym.pojo.vo.PageVo;
import com.nkym.pojo.vo.UserInterfaceVo;
import com.nkym.service.UserInterfaceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 19637
 * @description 针对表【user_interfaceInfo】的数据库操作Service实现
 * @createDate 2023-04-23 18:20:48
 */
@Service
public class UserInterfaceServiceImpl extends ServiceImpl<UserInterfaceMapper, UserInterface>
        implements UserInterfaceService {
    @Resource
    InterfaceInfoMapper interfaceInfoMapper;
    @Resource
    UserInterfaceMapper userInterfaceMapper;

    @Override
    public PageVo<UserInterfaceVo> pageQueryInterface(InterfaceInfoQueryRequest infoQueryRequest, HttpServletRequest request) {
        if (infoQueryRequest.getPageNow() == null || infoQueryRequest.getPageSize() == null) {
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        }
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(UserConstant.USER_LOGIN_STATUS);
        if (user == null) {
            throw new BusinessException(ErrorCode.USER_UNLOGIN);
        }


        Integer userId = user.getId();
        IPage<UserInterface> page = new Page<>(infoQueryRequest.getPageNow(), infoQueryRequest.getPageSize());
        QueryWrapper<UserInterface> wrapper = new QueryWrapper<>();
        wrapper.eq("userId", userId);
        IPage<UserInterface> userInterfacePage = userInterfaceMapper.selectPage(page, wrapper);
        List<UserInterface> records = userInterfacePage.getRecords();
        List<UserInterfaceVo> res = new ArrayList<>();


        for (UserInterface record : records) {
            InterfaceInfo interfaceInfo = interfaceInfoMapper.getInterfaceInfoById(record.getInterfaceInfoId());
            if (interfaceInfo == null) continue;
            UserInterfaceVo vo = new UserInterfaceVo();
            vo.setDescription(interfaceInfo.getDescription());
            vo.setInterfaceInfoId(interfaceInfo.getId());
            vo.setName(interfaceInfo.getName());
            vo.setTotalNum(record.getTotalNum());
            vo.setLeftNum(record.getLeftNum());
            res.add(vo);
        }
        PageVo<UserInterfaceVo> pageVo = new PageVo<>();
        pageVo.setCurrent(userInterfacePage.getCurrent());
        pageVo.setSize(userInterfacePage.getSize());
        pageVo.setData(res);
        pageVo.setTotal(userInterfacePage.getTotal());
        return pageVo;
    }

    @Override
    @Transactional
    public boolean assign(UserInterfaceAssignRequest assignRequest, HttpServletRequest request) {
        Integer interfaceInfoId = assignRequest.getId();
        Integer number = assignRequest.getNumber();
        if (interfaceInfoId == null) {
            throw new BusinessException(ErrorCode.PARAM_EMPTY);
        }

        //查询接口是否存在
        QueryWrapper<InterfaceInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("id", interfaceInfoId);
        Long count = interfaceInfoMapper.selectCount(wrapper);
        if (count <= 0) {
            throw new BusinessException(ErrorCode.INTERFACE_EMPTY);
        }

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(UserConstant.USER_LOGIN_STATUS);
        if (user == null) {
            throw new BusinessException(ErrorCode.USER_UNLOGIN);
        }
        Integer userId = user.getId();
        QueryWrapper<UserInterface> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("interfaceInfoId", interfaceInfoId);
        Long selectCount = userInterfaceMapper.selectCount(queryWrapper);
        if (selectCount >= 1) {
            throw new BusinessException(ErrorCode.INTERFACE_ALREADY_ASSIGN);
        }
        UserInterface userInterface = new UserInterface();
        userInterface.setUserId(userId);
        userInterface.setLeftNum(number);
        userInterface.setInterfaceInfoId(interfaceInfoId);
        userInterfaceMapper.assign(userInterface);

        Integer userInterfaceInfoId = userInterface.getId();
        return userInterfaceInfoId > 0;
    }
}




